<template>
  <div>
<el-form style="width: 60%" ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
  <el-form-item label="图书名称" prop="name">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
   <el-form-item label="作者" prop="author">
    <el-input v-model="ruleForm.author"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
  </div>
</template>

<script>
    import axios from 'axios'

  export default {
    name: 'AddBook',
    data() {
      return {
        ruleForm: {
          name: '',
          author: '',
        },
         rules: {
          name: [
            { required: true, message: '图书名称不能为空', trigger: 'blur' },
          ],
          author: [
            { required: true, message: '作者不能为空', trigger: 'blur' },
          ],
    },
      }
    },
   
    methods: {
      submitForm(formName) {
        const _this = this 
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('http://localhost:81/book/save/', this.ruleForm).then(function (resp){
                if (resp.data == 'success') {
                  // alert('添加成功')
                  _this.$alert('《' + _this.ruleForm.name + '》添加成功！', '消息', {
                    confirmButtonText: '确定',
                    callback: action => {
                      _this.$router.push('/BookManager')
                  }
                });
                }
            })
          } else {
            return false
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>